{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# List Comprehensions\n",
    "\n",
    "**Time**\n",
    "- teaching: 10 min\n",
    "- exercises: 10 min\n",
    "\n",
    "**Questions**:\n",
    "- \"Is there a faster way to generate a new list by changing all the elements in an old one?\"\n",
    "- \"How can I write cleaner, more compact code?\"\n",
    "\n",
    "**Learning Objectives**:\n",
    "- \"Understand and write a `list comprehension`.\"\n",
    "* * * * *\n",
    "\n",
    "****"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# List Comprehensions are another way of doing loops with accumulation\n",
    "\n",
    "- First, let's look at how we would create a \"transformed\" version of a list with loops and the \"accumulation\" pattern."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Multiply every number in a list by 2 using a for loop\n",
    "nums1 = [5, 1, 3, 10]\n",
    "nums2 = []\n",
    "for x in nums1:\n",
    "    nums2.append(x * 2)\n",
    "    \n",
    "print(nums2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- Python has another way to perform iteration called `list comprehensions`, which is shorter and more compact. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Multiply every number in a list by 2 using a list comprehension\n",
    "nums2 = [x * 2 for x in nums1]\n",
    "\n",
    "print(nums2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## List comprehensions can incorporate conditional logic\n",
    "\n",
    "- What if we also have some conditional logic?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Multiply every number in a list by 2, but only if the number is greater than 4\n",
    "nums1 = [5, 1, 3, 10]\n",
    "nums2 = []\n",
    "for x in nums1:\n",
    "    if x > 4:\n",
    "        nums2.append(x * 2)\n",
    "    \n",
    "print(nums2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# And using a list comprehension\n",
    "nums2 = [x * 2 for x in nums1 if x > 4]\n",
    "\n",
    "print(nums2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are several advantages to list comprehensions, most obvious being cleaner, more readable code. List comprehensions also save variable name space if you are looking to modify elements in a list. Less obvious is that list comprehensions are actually calculated faster than `for` loops!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  Challenge 1: Convert lists\n",
    "\n",
    "Convert the following code to list comprehensions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Filter elements greater than 4\n",
    "a = [3, 4, 5]\n",
    "b = []\n",
    "for i in a:\n",
    "    if i > 4:\n",
    "        b.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Add three to all list members.\n",
    "a = [3, 4, 5]\n",
    "for i in range(len(a)):\n",
    "    a[i] += 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Convert items using a dictionary\n",
    "conversions = {'a': 1, 'b': 2, 'c': 3}\n",
    "l = ['a', 'b', 'c']\n",
    "converted_l = []\n",
    "for i in l:\n",
    "    converted_l.append(conversions[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 2: Dictionary comprehensions?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Can you use the same list comprehension syntax to increment all values in the `conversions` dictionary above by 1, assigning it to a new dictionary?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
